// pages/ce/hah.js
var app = getApp()

var axios = require("../util/request.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName:'',
	  password:'',
    usertype: false,
    passtype: false,
    assoustype: false,
    assousneme:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var username = wx.getStorageSync('userNam')
    this.setData({
      userName: username.userName,
      password: username.password
    })
    
    // console.log(app.data.code)
  },
  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
	password:function(e){
		// console.log(e.detail.value)
		this.setData({
			password:e.detail.value
		})
	},
  clickbtn: function(){
   
    const that = this;
    if (this.data.userName == '' || this.data.userName == null || this.data.userName==undefined){
      this.setData({
        usertype:true
      })
      setTimeout(()=>{
        that.setData({
          usertype: false
        })
      },1500)
    } else if (this.data.password == '' || this.data.password == null || this.data.password == undefined){
      this.setData({
        passtype: true
      })
      setTimeout(function () {
        that.setData({
          passtype: false
        })
      }, 1500)
    }else{
      wx.showLoading({
        title: '加载中...',
        mask: true,
      })
     
      wx.request({
        url: `${axios.host}auth_manager/plogin`,
        method:'post',
        data:{
          'account': this.data.userName,
          'address': "",
          'device': "",
          'ip': "",
          'password': this.data.password,
          'utc': "",
          'openid': app.data.code,
        },
        success: function (res) {
          wx.clearStorage()
          wx.hideLoading()
          // console.log(res)
          // console.log(res.data.data.token)
          wx.setStorage({
            key: 'userNam',
            data: {
              userName: that.data.userName,
              password: that.data.password,
            },
          })
          if (res.data.code == 'E000000') { 
            wx.setStorage({
              key: 'apply1',
              data: res.data.data.token,
            }) 
            app.globalData.token = res.data.data.token  
           wx.request({
             url: `${axios.host}auth_manager/user_info?token=${res.data.data.token}`,
             method:'get',
             header:{
               "Authorization": res.data.data.token
             },
             success(res){
              //  console.log(res.data.data.account_type)
               if (res.data.data.account_type=='100'){

               } else if (res.data.data.account_type == '200'){
                 wx.redirectTo({
                   url: `/pages/indexmd/indexmd?id=200`,
                 })
                //  center
               } else if (res.data.data.account_type == '300') {
                //  shop
                 wx.redirectTo({
                   url: `/pages/index/index?id=300`,
                 })
               
               } else if (res.data.data.account_type == '210') {
                 wx.redirectTo({
                   url: `/pages/index1/index1?id=210`,
                 })
               } else if (res.data.data.account_type == '220') {

               } else if (res.data.data.account_type == '230') {
                 
               }
             }
           })

                
            // wx.navigateTo({
            //   url: '/pages/index/index',
            // })
           
          } else {
          
            that.setData({
              assoustype: true,
              assousneme: res.data.desc,
            })
            setTimeout(function () {
              that.setData({
                assoustype: false
              })
            }, 1500)
           
          }
        }

      })
    }
    // console.log(this.data.userName)
  },
  shuffleSuc(res){
    // console.log(res)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})